package com.inei.cie.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.inei.cie.entidades.PCar;
import com.inei.cie.entidades.PCar_C_1N;

public class CaratulaDao {
	private UtilDB util;
	SQLiteDatabase db;
	Cursor cursor = null;

	public CaratulaDao(Context context) {
		util = new UtilDB(context);
	}

	public PCar ObtenerCaratula(String id_local, int Nro_Pred) {
		PCar pcar = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from PCar where id_local=? and PC_F_1=?",
				new String[] { id_local, "" + Nro_Pred });
		if (cursor.moveToFirst()) {
			pcar = new PCar();
			while (!cursor.isAfterLast()) {
				pcar.setId_local(cursor.getString(cursor
						.getColumnIndex("id_local")));
				pcar.setPC_F_1(cursor.getInt(cursor.getColumnIndex("PC_F_1")));
				pcar.setPC_CedAdic(cursor.getInt(cursor
						.getColumnIndex("PC_CedAdic")));
				pcar.setPC_A_1_Dep(cursor.getInt(cursor
						.getColumnIndex("PC_A_1_Dep")));
				pcar.setPC_A_2_Prov(cursor.getInt(cursor
						.getColumnIndex("PC_A_2_Prov")));
				pcar.setPC_A_3_Dist(cursor.getInt(cursor
						.getColumnIndex("PC_A_3_Dist")));
				pcar.setPC_A_4_CentroP(cursor.getString(cursor
						.getColumnIndex("PC_A_4_CentroP")));
				pcar.setPC_A_5_NucleoUrb(cursor.getString(cursor
						.getColumnIndex("PC_A_5_NucleoUrb")));
				pcar.setPC_A_6_Ugel(cursor.getString(cursor
						.getColumnIndex("PC_A_6_Ugel")));
				pcar.setPC_A_7Dir_1_Tvia(cursor.getInt(cursor
						.getColumnIndex("PC_A_7Dir_1_Tvia")));
				pcar.setPC_A_7Dir_2_Nomb(cursor.getString(cursor
						.getColumnIndex("PC_A_7Dir_2_Nomb")));
				pcar.setPC_A_7Dir_3_Nro(cursor.getString(cursor
						.getColumnIndex("PC_A_7Dir_3_Nro")));
				pcar.setPC_A_7Dir_4_Piso(cursor.getString(cursor
						.getColumnIndex("PC_A_7Dir_4_Piso")));
				pcar.setPC_A_7Dir_5_Mz(cursor.getString(cursor
						.getColumnIndex("PC_A_7Dir_5_Mz")));
				pcar.setPC_A_7Dir_6_Lt(cursor.getString(cursor
						.getColumnIndex("PC_A_7Dir_6_Lt")));
				pcar.setPC_A_7Dir_7_Sect(cursor.getString(cursor
						.getColumnIndex("PC_A_7Dir_7_Sect")));
				pcar.setPC_A_7Dir_8_Zona(cursor.getString(cursor
						.getColumnIndex("PC_A_7Dir_8_Zona")));
				pcar.setPC_A_7Dir_9_Et(cursor.getString(cursor
						.getColumnIndex("PC_A_7Dir_9_Et")));
				pcar.setPC_A_7Dir_10_Km(cursor.getString(cursor
						.getColumnIndex("PC_A_7Dir_10_Km")));
				pcar.setPC_A_8_DirVerif(cursor.getInt(cursor
						.getColumnIndex("PC_A_8_DirVerif")));
				pcar.setPC_A_9_RefDir(cursor.getString(cursor
						.getColumnIndex("PC_A_9_RefDir")));
				pcar.setPC_B_1_CodLocal(cursor.getString(cursor
						.getColumnIndex("PC_B_1_CodLocal")));
				pcar.setPC_B_2_CantEv(cursor.getInt(cursor
						.getColumnIndex("PC_B_2_CantEv")));

				pcar.setPC_C_2_Rfinal_fecha(cursor.getString(cursor
						.getColumnIndex("PC_C_2_Rfinal_fecha")));
				pcar.setPC_C_2_Rfinal_resul(cursor.getInt(cursor
						.getColumnIndex("PC_C_2_Rfinal_resul")));
				pcar.setPC_C_2_Rfinal_resul_O(cursor.getString(cursor
						.getColumnIndex("PC_C_2_Rfinal_resul_O")));

				pcar.setPC_D_EvT_dni(cursor.getString(cursor
						.getColumnIndex("PC_D_EvT_dni")));
				pcar.setPC_D_EvT_Nomb(cursor.getString(cursor
						.getColumnIndex("PC_D_EvT_Nomb")));
				pcar.setPC_D_JBri_dni(cursor.getString(cursor
						.getColumnIndex("PC_D_JBri_dni")));
				pcar.setPC_D_JBri_Nomb(cursor.getString(cursor
						.getColumnIndex("PC_D_JBri_Nomb")));
				pcar.setPC_D_CProv_dni(cursor.getString(cursor
						.getColumnIndex("PC_D_CProv_dni")));
				pcar.setPC_D_CProv_Nomb(cursor.getString(cursor
						.getColumnIndex("PC_D_CProv_Nomb")));
				pcar.setPC_D_CDep_dni(cursor.getString(cursor
						.getColumnIndex("PC_D_CDep_dni")));
				pcar.setPC_D_CDep_Nomb(cursor.getString(cursor
						.getColumnIndex("PC_D_CDep_Nomb")));
				pcar.setPC_D_SupN_dni(cursor.getString(cursor
						.getColumnIndex("PC_D_SupN_dni")));
				pcar.setPC_D_SupN_Nomb(cursor.getString(cursor
						.getColumnIndex("PC_D_SupN_Nomb")));

				pcar.setPC_E_1_TPred(cursor.getInt(cursor
						.getColumnIndex("PC_E_1_TPred")));
				pcar.setPC_E_2_TPred_NoCol(cursor.getInt(cursor
						.getColumnIndex("PC_E_2_TPred_NoCol")));
				pcar.setPC_E_3_TEdif(cursor.getInt(cursor
						.getColumnIndex("PC_E_3_TEdif")));
				pcar.setPC_E_4_TPat(cursor.getInt(cursor
						.getColumnIndex("PC_E_4_TPat")));
				pcar.setPC_E_5_TLosa(cursor.getInt(cursor
						.getColumnIndex("PC_E_5_TLosa")));
				pcar.setPC_E_6_TCist(cursor.getInt(cursor
						.getColumnIndex("PC_E_6_TCist")));
				pcar.setPC_E_7_TMurCon(cursor.getInt(cursor
						.getColumnIndex("PC_E_7_TMurCon")));

				pcar.setSwCompleto(cursor.getInt(cursor
						.getColumnIndex("SwCompleto")));
				cursor.moveToNext();
			}
		}
		Cerrar();
		return pcar;
	}

	public boolean ExisteCaratula(PCar pcar) {
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from PCar where id_local=? and PC_F_1=?",
				new String[] { pcar.getId_local(), pcar.getPC_F_1() + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}

	}

	public boolean GuardarCaratula(PCar pcar) {
		try {
			ContentValues contenido = new ContentValues();
			contenido.put("PC_F_1", pcar.getPC_F_1());
			contenido.put("PC_CedAdic", pcar.getPC_CedAdic());
			contenido.put("PC_A_1_Dep", pcar.getPC_A_1_Dep());
			contenido.put("PC_A_2_Prov", pcar.getPC_A_2_Prov());
			contenido.put("PC_A_3_Dist", pcar.getPC_A_3_Dist());
			contenido.put("PC_A_4_CentroP", pcar.getPC_A_4_CentroP());
			contenido.put("PC_A_5_NucleoUrb", pcar.getPC_A_5_NucleoUrb());
			contenido.put("PC_A_6_Ugel", pcar.getPC_A_6_Ugel());
			contenido.put("PC_A_7Dir_1_Tvia", pcar.getPC_A_7Dir_1_Tvia());
			contenido.put("PC_A_7Dir_2_Nomb", pcar.getPC_A_7Dir_2_Nomb());
			contenido.put("PC_A_7Dir_3_Nro", pcar.getPC_A_7Dir_3_Nro());
			contenido.put("PC_A_7Dir_4_Piso", pcar.getPC_A_7Dir_4_Piso());
			contenido.put("PC_A_7Dir_5_Mz", pcar.getPC_A_7Dir_5_Mz());
			contenido.put("PC_A_7Dir_6_Lt", pcar.getPC_A_7Dir_6_Lt());
			contenido.put("PC_A_7Dir_7_Sect", pcar.getPC_A_7Dir_7_Sect());
			contenido.put("PC_A_7Dir_8_Zona", pcar.getPC_A_7Dir_8_Zona());
			contenido.put("PC_A_7Dir_9_Et", pcar.getPC_A_7Dir_9_Et());
			contenido.put("PC_A_7Dir_10_Km", pcar.getPC_A_7Dir_10_Km());
			contenido.put("PC_A_8_DirVerif", pcar.getPC_A_8_DirVerif());
			contenido.put("PC_A_9_RefDir", pcar.getPC_A_9_RefDir());
			contenido.put("PC_B_1_CodLocal", pcar.getPC_B_1_CodLocal());
			contenido.put("PC_B_2_CantEv", pcar.getPC_B_2_CantEv());

			contenido.put("PC_C_2_Rfinal_fecha", pcar.getPC_C_2_Rfinal_fecha());
			contenido.put("PC_C_2_Rfinal_resul", pcar.getPC_C_2_Rfinal_resul());
			contenido.put("PC_C_2_Rfinal_resul_O",
					pcar.getPC_C_2_Rfinal_resul_O());

			contenido.put("PC_D_EvT_dni", pcar.getPC_D_EvT_dni());
			contenido.put("PC_D_EvT_Nomb", pcar.getPC_D_EvT_Nomb());
			contenido.put("PC_D_JBri_dni", pcar.getPC_D_JBri_dni());
			contenido.put("PC_D_JBri_Nomb", pcar.getPC_D_JBri_Nomb());
			contenido.put("PC_D_CProv_dni", pcar.getPC_D_CProv_dni());
			contenido.put("PC_D_CProv_Nomb", pcar.getPC_D_CProv_Nomb());
			contenido.put("PC_D_CDep_dni", pcar.getPC_D_CDep_dni());
			contenido.put("PC_D_CDep_Nomb", pcar.getPC_D_CDep_Nomb());
			contenido.put("PC_D_SupN_dni", pcar.getPC_D_SupN_dni());
			contenido.put("PC_D_SupN_Nomb", pcar.getPC_D_SupN_Nomb());
			contenido.put("PC_E_1_TPred", pcar.getPC_E_1_TPred());
			contenido.put("PC_E_2_TPred_NoCol", pcar.getPC_E_2_TPred_NoCol());
			contenido.put("PC_E_3_TEdif", pcar.getPC_E_3_TEdif());
			contenido.put("PC_E_4_TPat", pcar.getPC_E_4_TPat());
			contenido.put("PC_E_5_TLosa", pcar.getPC_E_5_TLosa());
			contenido.put("PC_E_6_TCist", pcar.getPC_E_6_TCist());
			contenido.put("PC_E_7_TMurCon", pcar.getPC_E_7_TMurCon());
			contenido.put("Version", pcar.getVersion());
			contenido.put("SwCompleto", pcar.getSwCompleto());
			if (pcar.getPC_F_1() != 1) {
				contenido.remove("PC_A_8_DirVerif");
				contenido.remove("PC_B_1_CodLocal");
				contenido.remove("PC_B_2_CantEv");
			}
			if (ExisteCaratula(pcar)) {
				db = util.getWritableDatabase();
				db.update(
						"PCar",
						contenido,
						"id_local=? and PC_F_1=?",
						new String[] { pcar.getId_local(),
								"" + pcar.getPC_F_1() });
			} else {
				contenido.put("id_local", pcar.getId_local());
				db = util.getWritableDatabase();
				db.insert("PCar", null, contenido);
			}

			db.close();
		} catch (Exception e) {
			db.close();
			return false;
		}
		return true;
	}

	public List<PCar_C_1N> ObtenerListaVisitas(String id_local, int PC_F_1) {
		List<PCar_C_1N> lista = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from PCar_C_1N where id_local=? and PC_F_1=?",
				new String[] { id_local, PC_F_1 + "" });
		if (cursor.moveToFirst()) {
			lista = new ArrayList<PCar_C_1N>();
			while (!cursor.isAfterLast()) {
				PCar_C_1N obj = new PCar_C_1N();
				obj.setId_local(id_local);
				obj.setPC_F_1(PC_F_1);
				obj.setPC_C_1_NroVis(cursor.getInt(cursor
						.getColumnIndex("PC_C_1_NroVis")));
				obj.setPC_C_1_Et_Fecha(cursor.getString(cursor
						.getColumnIndex("PC_C_1_Et_Fecha")));
				obj.setPC_C_1_Et_Hini(cursor.getString(cursor
						.getColumnIndex("PC_C_1_Et_Hini")));
				obj.setPC_C_1_Et_Hfin(cursor.getString(cursor
						.getColumnIndex("PC_C_1_Et_Hfin")));
				obj.setPC_C_1_Et_Fecha_Prox(cursor.getString(cursor
						.getColumnIndex("PC_C_1_Et_Fecha_Prox")));
				obj.setPC_C_1_Et_Hora_Prox(cursor.getString(cursor
						.getColumnIndex("PC_C_1_Et_Hora_Prox")));
				obj.setPC_C_1_Et_Res(cursor.getInt(cursor
						.getColumnIndex("PC_C_1_Et_Res")));
				obj.setPC_C_1_Et_Res_O(cursor.getString(cursor
						.getColumnIndex("PC_C_1_Et_Res_O")));
				obj.setPC_C_1_Jb_Fecha(cursor.getString(cursor
						.getColumnIndex("PC_C_1_Jb_Fecha")));
				obj.setPC_C_1_Jb_Hini(cursor.getString(cursor
						.getColumnIndex("PC_C_1_Jb_Hini")));
				obj.setPC_C_1_Jb_Hfin(cursor.getString(cursor
						.getColumnIndex("PC_C_1_Jb_Hfin")));
				obj.setPC_C_1_Jb_Res(cursor.getInt(cursor
						.getColumnIndex("PC_C_1_Jb_Res")));
				obj.setPC_C_1_Jb_Res_O(cursor.getString(cursor
						.getColumnIndex("PC_C_1_Jb_Res_O")));
				obj.setSwCompleto(cursor.getInt(cursor
						.getColumnIndex("SwCompleto")));
				obj.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
				lista.add(obj);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return lista;
	}

	public PCar_C_1N ObtenerVisita(String id_local, int PC_F_1, int nro) {
		PCar_C_1N obj = null;
		db = util.getReadableDatabase();
		cursor = db
				.rawQuery(
						"select * from PCar_C_1N where id_local=? and PC_F_1=? and PC_C_1_NroVis=?",
						new String[] { id_local, PC_F_1 + "", nro + "" });
		if (cursor.moveToFirst()) {
			obj = new PCar_C_1N();
			obj.setId_local(id_local);
			obj.setPC_F_1(PC_F_1);
			obj.setPC_C_1_NroVis(cursor.getInt(cursor
					.getColumnIndex("PC_C_1_NroVis")));
			obj.setPC_C_1_Et_Fecha(cursor.getString(cursor
					.getColumnIndex("PC_C_1_Et_Fecha")));
			obj.setPC_C_1_Et_Hini(cursor.getString(cursor
					.getColumnIndex("PC_C_1_Et_Hini")));
			obj.setPC_C_1_Et_Hfin(cursor.getString(cursor
					.getColumnIndex("PC_C_1_Et_Hfin")));
			obj.setPC_C_1_Et_Fecha_Prox(cursor.getString(cursor
					.getColumnIndex("PC_C_1_Et_Fecha_Prox")));
			obj.setPC_C_1_Et_Hora_Prox(cursor.getString(cursor
					.getColumnIndex("PC_C_1_Et_Hora_Prox")));
			obj.setPC_C_1_Et_Res(cursor.getInt(cursor
					.getColumnIndex("PC_C_1_Et_Res")));
			obj.setPC_C_1_Et_Res_O(cursor.getString(cursor
					.getColumnIndex("PC_C_1_Et_Res_O")));
			obj.setPC_C_1_Jb_Fecha(cursor.getString(cursor
					.getColumnIndex("PC_C_1_Jb_Fecha")));
			obj.setPC_C_1_Jb_Hini(cursor.getString(cursor
					.getColumnIndex("PC_C_1_Jb_Hini")));
			obj.setPC_C_1_Jb_Hfin(cursor.getString(cursor
					.getColumnIndex("PC_C_1_Jb_Hfin")));
			obj.setPC_C_1_Jb_Res(cursor.getInt(cursor
					.getColumnIndex("PC_C_1_Jb_Res")));
			obj.setPC_C_1_Jb_Res_O(cursor.getString(cursor
					.getColumnIndex("PC_C_1_Jb_Res_O")));
			obj.setSwCompleto(cursor.getInt(cursor.getColumnIndex("SwCompleto")));
			obj.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
		}

		Cerrar();
		return obj;
	}

	public boolean GuardarVisita(PCar_C_1N visita) {
		try {
			ContentValues c = new ContentValues();
			c.put("PC_C_1_Et_Fecha", visita.getPC_C_1_Et_Fecha());
			c.put("PC_C_1_Et_Hini", visita.getPC_C_1_Et_Hini());
			c.put("PC_C_1_Et_Hfin", visita.getPC_C_1_Et_Hfin());
			c.put("PC_C_1_Et_Fecha_Prox", visita.getPC_C_1_Et_Fecha_Prox());
			c.put("PC_C_1_Et_Hora_Prox", visita.getPC_C_1_Et_Hora_Prox());
			c.put("PC_C_1_Et_Res", visita.getPC_C_1_Et_Res());
			c.put("PC_C_1_Et_Res_O", visita.getPC_C_1_Et_Res_O());
			c.put("PC_C_1_Jb_Fecha", visita.getPC_C_1_Jb_Fecha());
			c.put("PC_C_1_Jb_Hini", visita.getPC_C_1_Jb_Hini());
			c.put("PC_C_1_Jb_Hfin", visita.getPC_C_1_Jb_Hfin());
			c.put("PC_C_1_Jb_Res", visita.getPC_C_1_Jb_Res());
			c.put("PC_C_1_Jb_Res_O", visita.getPC_C_1_Jb_Res_O());
			c.put("Version", visita.getVersion());
			c.put("SwCompleto", visita.getSwCompleto());
			if (ExisteVisita(visita)) {
				db = util.getWritableDatabase();
				db.update(
						"PCar_C_1N",
						c,
						"id_local=? and PC_F_1=? and PC_C_1_NroVis=?",
						new String[] { visita.getId_local(),
								visita.getPC_F_1() + "",
								visita.getPC_C_1_NroVis() + "" });
			} else {
				db = util.getWritableDatabase();
				c.put("id_local", visita.getId_local());
				c.put("PC_F_1", visita.getPC_F_1());
				c.put("PC_C_1_NroVis", visita.getPC_C_1_NroVis());
				db.insert("PCar_C_1N", null, c);
			}
			db.close();
		} catch (Exception e) {
			db.close();
			return false;
		}
		return true;
	}

	public boolean ExisteVisita(PCar_C_1N visita) {
		db = util.getReadableDatabase();
		cursor = db
				.rawQuery(
						"select * from PCar_C_1N where id_local=? and PC_F_1=? and PC_C_1_NroVis=?",
						new String[] { visita.getId_local(),
								visita.getPC_F_1() + "",
								visita.getPC_C_1_NroVis() + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public int totalPredios(String id_local) {
		int nroPredios = 0;
		db = util.getReadableDatabase();
		cursor = db.rawQuery("select * from P1_B_2A_N where id_local=?",
				new String[] { id_local + "" });
		if (cursor.moveToFirst()) {
			nroPredios = cursor.getCount();
			Cerrar();
			return nroPredios;
		} else {
			Cerrar();
			return nroPredios;
		}
	}

	public int totalPrediosNoColindante(String id_local) {
		int nroPrediosNo = 0;
		db = util.getReadableDatabase();
		cursor = db
				.rawQuery(
						"select * from P1_B_2A_N where id_local=? and P1_B_2A_PredNoCol=1",
						new String[] { id_local + "" });
		if (cursor.moveToFirst()) {
			nroPrediosNo = cursor.getCount();
			Cerrar();
			return nroPrediosNo;
		} else {
			Cerrar();
			return nroPrediosNo;
		}
	}

	public int totalEdificaciones(String id_local, int Nro_Pred) {
		int nroEdificaciones = 0;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P5 where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				nroEdificaciones = cursor.getInt(cursor
						.getColumnIndex("P5_Tot_E"));
				cursor.moveToNext();
			}
			Cerrar();
			return nroEdificaciones;
		} else {
			Cerrar();
			return nroEdificaciones;
		}
	}

	public int totalPatios(String id_local, int Nro_Pred) {
		int nroPatios = 0;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P5 where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				nroPatios += cursor.getInt(cursor.getColumnIndex("P5_Tot_P"));
				cursor.moveToNext();
			}
			Cerrar();
			return nroPatios;
		} else {
			Cerrar();
			return nroPatios;
		}
	}

	public int totalLosasDeportivas(String id_local, int Nro_Pred) {
		int nroLosasDeportivas = 0;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P5 where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				nroLosasDeportivas += cursor.getInt(cursor
						.getColumnIndex("P5_Tot_LD"));
				cursor.moveToNext();
			}
			Cerrar();
			return nroLosasDeportivas;
		} else {
			Cerrar();
			return nroLosasDeportivas;
		}
	}

	public int totalCisternas(String id_local, int Nro_Pred) {
		int nroCisternas = 0;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P5 where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				nroCisternas += cursor.getInt(cursor
						.getColumnIndex("P5_Tot_CTE"));
				cursor.moveToNext();
			}
			Cerrar();
			return nroCisternas;
		} else {
			Cerrar();
			return nroCisternas;
		}
	}

	public int totalMuros(String id_local, int Nro_Pred) {
		int nroMuros = 0;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P5 where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				nroMuros += cursor.getInt(cursor.getColumnIndex("P5_Tot_MC"));
				cursor.moveToNext();
			}
			Cerrar();
			return nroMuros;
		} else {
			Cerrar();
			return nroMuros;
		}
	}

	public boolean eliminarVisita(String id_local, int pred, int pos) {
		try {
			List<PCar_C_1N> visitas = ObtenerListaVisitas(id_local, pred);
			db = util.getWritableDatabase();
			db.delete("PCar_C_1N",
					"id_local=? and PC_F_1=? and PC_C_1_NroVis=?",
					new String[] { id_local, pred + "", pos + "" });
			if (visitas != null) {
				for (int i = pos; i < visitas.size(); i++) {
					PCar_C_1N visita = visitas.get(i);
					ContentValues c = new ContentValues();
					c.put("PC_C_1_NroVis", i);
					db.update("PCar_C_1N", c,
							"id_local=? and PC_F_1=? and PC_C_1_NroVis=?",
							new String[] { id_local, pred + "", (i + 1) + "" });
				}
				db.close();
			}
			return true;
		} catch (Exception e) {
			db.close();
			return false;
		}
	}

	protected void Cerrar() {
		cursor.close();
		db.close();
	}

}